import {
  CanActivate,
  ExecutionContext,
  Inject,
  Injectable,
} from '@nestjs/common';
import { Observable } from 'rxjs';
// import { UserService } from 'src/user/user.service';
import * as jwt from 'jsonwebtoken';

@Injectable()
export class WsGuard implements CanActivate {
  constructor() {}

  // @Inject()
  // private userService: UserService;

  canActivate(
    context: ExecutionContext,
  ): boolean | any | Promise<boolean | any> | Observable<boolean | any> {
    // const bearerToken: { args: Array<any> } =
    //   context.args[0].handshake.headers.authorization.split(' ')[1];
    console.log('=====findByUsername bearerToken');
    console.log('---headers');
    console.log(context.getArgs()[0].handshake.headers);
    const args = context.getArgs();
    const auth = args[0].handshake.headers.authorization;
    console.log('auth');
    console.log(auth);
    const bearerToken = auth && auth.split(' ')[1];
    console.log(bearerToken);
    // const findNameFn = this.userService.findByUsername;
    // console.log('findNameFn');
    return true;
    // try {
    //   const decoded = jwt.verify(bearerToken, jwtConstants.secret) as any;
    //   return new Promise((resolve, reject) => {
    //     return this.userService
    //       .findByUsername(decoded.username)
    //       .then((user) => {
    //         if (user) {
    //           resolve(user);
    //         } else {
    //           reject(false);
    //         }
    //       });
    //   });
    // } catch (ex) {
    //   console.log(ex);
    //   return false;
    // }
  }
}
